//管理员action实现类
package com.sy.action;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.action.ActionMessage;
import org.apache.struts.action.ActionMessages;

import com.sy.form.AdminForm;
import com.sy.form.LoginForm;
import com.sy.service.AdminService;
import com.sy.util.MD5;
import com.sy.util.Page;
import com.sy.vo.Admin;
@SuppressWarnings("unchecked")
public class AdminAction extends BaseAction {
//验证登陆
	public ActionForward islogin(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response) {
//spring读取实例化业务对象
		AdminService as=(AdminService)super.getBean("AdminService"); 
//struts读取表单数据
		LoginForm lf = (LoginForm) form;
		Admin admin=new Admin();
		admin.setAname(lf.getUsername());
		admin.setApassword(MD5.md5(lf.getPassword()));
		if(as.islogin(admin.getAname(),admin.getApassword())){
			//存session
			HttpSession session = request.getSession();
		    session.setAttribute("admin.username", lf.getUsername());
			request.setAttribute("admin",admin);
		}else{
			ActionMessages errors = new ActionMessages() ;
			errors.add("error",new ActionMessage("login.error")) ;
			super.saveErrors(request, errors) ;
			return mapping.getInputForward() ;
		}
		return mapping.findForward("login_suc");
	}
//注销
	public ActionForward logout(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response) {
//struts读取表单数据
		HttpSession session = request.getSession();
		if(null!=session.getAttribute("admin.username"))
			session.setAttribute("admin.username",null);
		return mapping.findForward("logout_suc");
	}
//添加管理员
	public ActionForward addAdmin(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response) {
		AdminService as=(AdminService)super.getBean("AdminService");
		AdminForm af = (AdminForm) form;
		
		Admin admin=new Admin();
		admin.setAname(af.getUsername());
		admin.setApassword(MD5.md5(af.getPassword()));
		as.addAdmin(admin);
		return mapping.findForward("add_suc");
	}
//修改管理员
	public ActionForward editAdmin(ActionMapping mapping,ActionForm form,
			HttpServletRequest request,HttpServletResponse respone){
		AdminService as=(AdminService)super.getBean("AdminService");
		AdminForm af = (AdminForm) form;
		Admin admin=as.getAdmin(Integer.valueOf(request.getParameter("aid")));
		af.setUsername(admin.getAname());
		af.setPassword(MD5.md5(admin.getApassword()));
		request.setAttribute("Admin.aid", admin.getAid());
		return mapping.findForward("edit_suc");
	}
//更新管理员
	public ActionForward updateAdmin(ActionMapping mapping,ActionForm form,
			HttpServletRequest request,HttpServletResponse respone){
		AdminService as=(AdminService)super.getBean("AdminService");
		AdminForm af = (AdminForm) form;
		Admin admin=new Admin();
		admin.setAid(Integer.valueOf(request.getParameter("aid")));
		admin.setAname(af.getUsername());
		admin.setApassword(MD5.md5(af.getPassword()));
		as.updateAdmin(admin);
		return mapping.findForward("update_suc");
	}
//删除管理员
	public ActionForward deleteAdmin(ActionMapping mapping, ActionForm form, HttpServletRequest request,
		    HttpServletResponse response) {
		AdminService as=(AdminService)super.getBean("AdminService");
		as.deleteAdmin(Integer.valueOf(request.getParameter("aid")));
		return mapping.findForward("delete_suc");
	    }
//查看管理员
	public ActionForward listAdmins(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response) {
		AdminService as=(AdminService)super.getBean("AdminService");
		   int pno = (new Integer(request.getParameter("pno"))).intValue();
		   as.init(pno);
		   Page myPage = as.getPage();
		   List myList = as.getPage().getList(); 
		   request.setAttribute("myPage", myPage);
		   request.setAttribute("AdminList", myList);
		   return mapping.findForward("show");
		   
	}
}